package com.greyu.ysj.service.impl;



import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.greyu.ysj.config.ResultStatus;
import com.greyu.ysj.entity.CbtInvitedIncome;
import com.greyu.ysj.entity.User;
import com.greyu.ysj.entity.UserExample;
import com.greyu.ysj.entity.CbtInvitedIncomeExample;
import com.greyu.ysj.mapper.CbtInvestMapper;
import com.greyu.ysj.mapper.CbtInvitedIncomeMapper;
import com.greyu.ysj.mapper.UserMapper;
import com.greyu.ysj.model.ResultModel;
import com.greyu.ysj.service.CbtInvitedIncomeService;
import com.greyu.ysj.service.UserService;

@Service
@Transactional
public class CbtInvitedIncomeServiceImpl implements CbtInvitedIncomeService {
	@Autowired
	private CbtInvitedIncomeMapper cbtInvitedIncomeMapper;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CbtInvestMapper cbtInvestMapper;
	
	@Override
	public ResultModel save(CbtInvitedIncome cbtInvitedIncome) {
		if(cbtInvitedIncome.getAmount() == null || cbtInvitedIncome.getFlag() == null
				|| cbtInvitedIncome.getRegisterNo() == null || cbtInvitedIncome.getUserId() == null) {
			return ResultModel.error(ResultStatus.DATA_NOT_NULL);
		}
		cbtInvitedIncome.setCreateDate(new Date());
		this.cbtInvitedIncomeMapper.insert(cbtInvitedIncome);
		return ResultModel.ok(cbtInvitedIncome);
	}
	@Override
	public double getAllRewardByuserId(Integer userId) {
		
		
			UserExample userExample = new UserExample();
	        UserExample.Criteria criteria = userExample.createCriteria();
            criteria.andRefererEqualTo(userId);
	        List<User> users = this.userMapper.selectByExample(userExample);
	        List<Integer> list = users.stream().map(u->u.getUserId()).collect(Collectors.toList());
	        double amount = 0.0;
	        if (list.size()>0) {
	        	 amount=this.cbtInvestMapper.selectTotalRewardByuserId(userId,list);
			}
	        amount = amount*0.1;
	
//		 amount=this.cbtInvitedIncomeMapper.selectTotalRewardByuserId(userId,list);
		return amount;
	}
	@Override
	public List<CbtInvitedIncome> selectAllByuserId(Integer page,Integer rows,Integer userId) {
		if (null != page && null != rows) {
            PageHelper.startPage(page, rows);
        }
		CbtInvitedIncomeExample CbtInvitedIncomeExample = new CbtInvitedIncomeExample();
        CbtInvitedIncomeExample.Criteria criteria = CbtInvitedIncomeExample.createCriteria();
//        criteria.andFlagEqualTo(0);
        criteria.andUserIdEqualTo(userId);
        CbtInvitedIncomeExample.setOrderByClause("`create_date` DESC");
        List<CbtInvitedIncome> list = this.cbtInvitedIncomeMapper.selectByExample(CbtInvitedIncomeExample);
     
		return list;
	}
	@Override
	public int totalPages(int rows, int userId) {
		int total=countByuserId(userId);
		int totalPages;
		if(total%rows == 0) {
			totalPages=(int) (total/rows);
		}else {
			totalPages=(int) (total/rows)+1;
		}
		return totalPages;
	}
	@Override
	public int countByuserId(int userId) {
		int count=this.cbtInvitedIncomeMapper.countByuserId(userId);
		return count;
	}
	@Override
	public double selectYesterdayIncomeByuserId(int userId) {
		// TODO Auto-generated method stub
		double yesterdayIncome=cbtInvitedIncomeMapper.selectYesterdayIncomeByuserId(userId);
		return yesterdayIncome;
	}
	@Override
	public double selectDirectRewardByuserId(Integer userId) {
		
		return cbtInvitedIncomeMapper.selectDirectRewardByuserId(userId);
	}
	@Override
	public double getAllIncomeByUserId(Integer userId) {
		
		
		return cbtInvitedIncomeMapper.getAllIncomeByUserId(userId);
	}
	@Override
	public double getYesterdayIncomeByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvitedIncomeMapper.getYesterdayIncomeByUserId(userId);
	}
	@Override
	public double getYesterdayInvIncomeByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvitedIncomeMapper.getYesterdayInvIncomeByUserId(userId);
	}
	
	@Override
	public double selectDirectIncomeByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvitedIncomeMapper.selectDirectIncomeByuserId(userId);
	}
	
	@Override
	public double selectInvIncomeByuserId(Integer userId) {
		// TODO Auto-generated method stub
		return cbtInvitedIncomeMapper.selectInvIncomeByuserId(userId);
	}

	
	
	
}
